package com.wan37.logic.handler.message;

import com.wan37.logic.component.Worker;
import com.wan37.logic.domain.vo.AllRoleVO;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.fighting.auto.AutoFighting;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.MsgHandler;
import com.wan37.logic.service.cache.SceneServiceCache;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.AllRoleLabel;
import com.wan37.protobuf.constant.CommonFighting;
import com.wan37.protobuf.message.client2server.game.GameFighting;

import java.util.Map;
import java.util.Set;

/**
 * @author : luoyong
 * @date : 2020-07-08 15:42
 **/
public class CGameAutoFightingHandler extends MsgHandler {
    private SceneServiceCache sceneServiceCache;
    private AutoFighting autoFighting;
    private RouteUtils routeUtils;

    /**
     * MsgHandler构造函数
     *
     * @param messageVO
     */
    public CGameAutoFightingHandler(MessageVO messageVO) {
        super(messageVO);
        this.sceneServiceCache = SpringContextHolder.getBean("sceneServiceCache");
        this.routeUtils = SpringContextHolder.getBean(routeUtils.getClass());
    }

    @Override
    public void execute(Worker worker) throws Exception {
        GameFighting.AutoFightingMsg autoFightingMsg = (GameFighting.AutoFightingMsg) messageVO.getMessage();
        int code = autoFightingMsg.getCode();
        String sceneId = sceneServiceCache.sceneNameToIdMap.get(autoFightingMsg.getSceneName());
        autoFightingMsg.getSenderIdList().forEach(sender -> {
            String fightingId = sender.getStringId();
            Set<String> userIdSet = sceneServiceCache.getSceneServiceMap().get(sceneId).getUserIdSet();
            Map<String, AllRoleVO> allRoleVOMap = sceneServiceCache.getSceneServiceMap().get(sceneId).getAllRoleVOMap();
            if (userIdSet.contains(fightingId)) {
                this.autoFighting = SpringContextHolder.getBean("autoPlayerService");
                this.autoFighting.fighting(sceneId, fightingId);
            } else if (allRoleVOMap.containsKey(sender) && allRoleVOMap.get(sender).getLabel() == AllRoleLabel.MONSTER) {
                this.autoFighting = SpringContextHolder.getBean("autoMonsterService");
            }
        });

    }
}
